#region File Header
//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
#endregion File Header

using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using System.Configuration;
using System.IO;
using System.Security;
using AirWeb.DirectoryMgr;
using AirWeb.ApplicationTools.Mail;
using System.Threading;
using AirWeb.Security.FileSecurity;
using Role=AirWeb.Security.Role;
using SecurityMgr=AirWeb.Security.SecurityMgr;
using User=AirWeb.Security.User;

namespace FlashBack.Web
{
	/// <summary>
	/// Summary description for SendEmails.
	/// </summary>
	public partial class SendEmails : PageBase
	{

		ArrayList m_RoleList = new ArrayList();

		MailHelper m_helper;

		public SendEmails()
		{
			Page.Init += new System.EventHandler(Page_Init);
		}

		protected void Page_Load(object sender, System.EventArgs e)
		{
			Response.Expires = -1;

			if(!CurrentUser.IsAdministrator())
			{
				throw new SecurityException("You are not authorized to view this page");
			}

			m_helper = new MailHelper("mailTemplateNew",this.Context);
			m_helper.TemplateSubject = "What's new in [ORIGINATOR_NAME]'s web site";

			// Build the users table
			if(!IsPostBack)
			{
				// Build the role array;
				foreach(Role r in SecurityMgr.Instance.Roles)
				{
					if(r == null)
						continue;

					m_RoleList.Add(r.Name);
				}

				BuildUserTable();

				body.Text = m_helper.TemplateBody;
				subject.Text = m_helper.TemplateSubject;
			}

			ErrorText.Visible = false;
		}

		/// <summary>
		/// Creates a data table with user content and binds it to the data grid control
		/// </summary>
		protected void BuildUserTable()
		{
			DataTable dt = new DataTable();
			DataRow dr;

			dt.Columns.Add(new DataColumn("Name", typeof(string)));
			dt.Columns.Add(new DataColumn("Email", typeof(string)));
			dt.Columns.Add(new DataColumn("Can Upload", typeof(string)));
			dt.Columns.Add(new DataColumn("Role", typeof(string)));

			foreach (User a in SecurityMgr.Instance.Users)
			{
				if(a == null)
					continue;

				dr = dt.NewRow();
				
				dr[0] = a.Name;
				dr[1] = a.Email;

				ExtraUserData eud = null;
				try
				{
					eud = (ExtraUserData)a.ExtraData;
				}
				catch
				{}

				if( eud != null && eud.CanUpload == true ) 
					dr[2] = "Yes";
				else
					dr[2] = "No";
 
				dr[3] = a.UserRole.Name;

				dt.Rows.Add(dr);
			}
            
			UsersGrid.DataSource = new DataView(dt);
			UsersGrid.DataBind();
		}

		protected void DisplayError(Exception ex)
		{
			ErrorText.Text = "Error executing request. " + ex.Message;				
			ErrorText.Visible = true;
		}

		protected void Sendmail(object sender, CommandEventArgs e)
		{
			m_helper.TemplateBody = body.Text;
			m_helper.TemplateSubject = subject.Text;

			int es = 0;

			foreach(TableRow t in UsersGrid.Items)
			{
				CheckBox c = (CheckBox)t.Cells[0].FindControl("select");
				if(c.Checked)
				{
					string user = t.Cells[1].Text;
					User u = SecurityMgr.Instance.GetUserByName(user);
					if( u != null )
					{
						m_helper.AddRecipient(u);
						c.Checked = false;
						es++;
					}
				}
			}

			if(es > 0)
			{
				// spawn a thread to send the e-mails
				WaitCallback wcb = new WaitCallback(m_helper.SendMail);
				ThreadPool.QueueUserWorkItem(wcb);

				ErrorText.ForeColor = System.Drawing.Color.Green;
				ErrorText.Text = es.ToString() + " emails were sent!";
				ErrorText.Visible = true;
			}
		}

		protected void onRowBound(object sender, DataGridItemEventArgs e)
		{
			if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				TableCell c = (TableCell)e.Item.Cells[0];

				if(e.Item.Cells[2].Text == "&nbsp;")
				{
					Control c1 = e.Item.Cells[0].FindControl("select");
					if(c1 != null)
						c1.Visible = false;
				}
			}
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
		}

		#region Web Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion
	}
}
